//
//  GKZYMajorEmploymentTableViewCell.m
//  GKZYAid
//
//  Created by tony on 16/3/8.
//  Copyright © 2016年 GKZYAid. All rights reserved.
//

#import "GKZYMajorEmploymentTableViewCell.h"
#import "City_listModel.h"
#import "Major_salary_year_listModel.h"
#import "Industry_info_listModel.h"
#import "MyDefine.h"
#import <CorePlot.h>
#import "GKZYMajorIndustryListView.h"

@interface GKZYMajorEmploymentTableViewCell ()<CPTPlotDataSource>

@property(nonatomic,strong)NSMutableDictionary *dataDictionary;
@property(nonatomic,strong)UILabel *title0;
@property(nonatomic,strong)UILabel *title1;
@property(nonatomic,strong)UILabel *title2;
@property(nonatomic,strong)UILabel *title3;
@property(nonatomic,strong)CPTGraphHostingView *hostView1;
@property(nonatomic,strong)CPTGraphHostingView *hostView2;
@property(nonatomic,strong) CPTXYGraph *xyGraph1;
@property(nonatomic,strong) CPTXYGraph *xyGraph2;
@property(nonatomic,strong)GKZYMajorIndustryListView *industryView0;
@property(nonatomic,strong)GKZYMajorIndustryListView *industryView1;
@property(nonatomic,strong)GKZYMajorIndustryListView *industryView2;
@property(nonatomic,strong)GKZYMajorIndustryListView *industryView3;
@property(nonatomic,strong)GKZYMajorIndustryListView *industryView4;
@property(nonatomic,strong)NSMutableArray *cityDataArr;

@end


@implementation GKZYMajorEmploymentTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{

    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
       
        [self addViews];
        
    }
    return self;
}


-(void)addViews{
 
    self.title0 = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, KmainScreenBoundsWidth, 40)];
    self.title0.text = @"    就业详情";
    self.title0.layer.borderColor = [[UIColor colorWithWhite:0.1 alpha:0.1]CGColor];
    self.title0.layer.borderWidth = 0.5f;
    [self.contentView addSubview:self.title0];
    
    self.title1 = [[UILabel alloc]initWithFrame:CGRectMake(0, 40, KmainScreenBoundsWidth, 30)];
    self.title1.text = @"专业毕业后十年内平均工资";
    self.title1.textColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    self.title1.font = [UIFont systemFontOfSize:14];
    self.title1.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.title1];
    
    self.hostView1 = [[CPTGraphHostingView alloc]initWithFrame:CGRectMake(0, 70, KmainScreenBoundsWidth, 240)];
    
    [self.contentView addSubview:self.hostView1];

    self.title2 = [[UILabel alloc]initWithFrame:CGRectMake(0, 310, KmainScreenBoundsWidth, 30)];
    self.title2.text = @"就业领域Top5(%)";
    self.title2.textColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    self.title2.font = [UIFont systemFontOfSize:14];
    self.title2.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.title2];
    
    self.industryView0 = [[GKZYMajorIndustryListView alloc]initWithFrame:CGRectMake(10, 350, (KmainScreenBoundsWidth-20)/5, 120)];
    [self.contentView addSubview:self.industryView0];
    
    self.industryView1 = [[GKZYMajorIndustryListView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.industryView0.frame), 350, (KmainScreenBoundsWidth-20)/5, 120)];
    [self.contentView addSubview:self.industryView1];

    self.industryView2 = [[GKZYMajorIndustryListView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.industryView1.frame), 350, (KmainScreenBoundsWidth-20)/5, 120)];
    [self.contentView addSubview:self.industryView2];

    self.industryView3 = [[GKZYMajorIndustryListView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.industryView2.frame), 350, (KmainScreenBoundsWidth-20)/5, 120)];
    [self.contentView addSubview:self.industryView3];
    
    self.industryView4 = [[GKZYMajorIndustryListView alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.industryView3.frame), 340, (KmainScreenBoundsWidth-20)/5, 120)];
    [self.contentView addSubview:self.industryView4];

    
    self.title3 = [[UILabel alloc]initWithFrame:CGRectMake(0, 590, KmainScreenBoundsWidth, 30)];
    self.title3.text = @"就业城市Top5";
    self.title3.textColor = [UIColor colorWithWhite:0.1 alpha:0.5];
    self.title3.font = [UIFont systemFontOfSize:14];
    self.title3.textAlignment = NSTextAlignmentCenter;
    [self.contentView addSubview:self.title3];
    
    self.hostView2 = [[CPTGraphHostingView alloc]initWithFrame:CGRectMake(0, 620, KmainScreenBoundsWidth, 240)];
    [self.contentView addSubview:self.hostView2];
    

}

-(void)layoutSubviews{
    
    [super layoutSubviews];
    [self makeData];
    self.title0.frame = CGRectMake(0, 0, KmainScreenBoundsWidth, 40);
    self.title1.frame = CGRectMake(0, 40, KmainScreenBoundsWidth, 30);
    self.xyGraph1.frame = CGRectMake(0, 0, KmainScreenBoundsWidth, 240);
    self.hostView1.frame = CGRectMake(0, 70, KmainScreenBoundsWidth, 240);
    [self prepareTheGraph];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 320, KmainScreenBoundsWidth, 5)];
    label.backgroundColor = [UIColor colorWithRed:229/256.0 green:229/256.0 blue:229/256.0 alpha:0.8];
    [self.contentView addSubview:label];
    
    self.title2.frame = CGRectMake(0, 330, KmainScreenBoundsWidth, 30);
    
    self.industryView0.frame = CGRectMake(10, 360, (KmainScreenBoundsWidth-20)/5, 120);
    NSArray *array = self.model.ind_info_list;
    Industry_info_listModel *data0 = array[0];
    float percent0 = [data0.ratio floatValue]*100;
    [self.industryView0 setTheTitleWithOrder:[NSString stringWithFormat:@"%d",1] andName:data0.industry_name Percent:[NSString stringWithFormat:@"%0.1f",percent0]];
    
    self.industryView1.frame = CGRectMake(CGRectGetMaxX(self.industryView0.frame), 360, (KmainScreenBoundsWidth-20)/5, 120);
    Industry_info_listModel *data1 = array[1];
    float percent1 = [data1.ratio floatValue]*100;
    [self.industryView1 setTheTitleWithOrder:[NSString stringWithFormat:@"%d",2] andName:data1.industry_name Percent:[NSString stringWithFormat:@"%0.1f",percent1]];
    
    self.industryView2.frame = CGRectMake(CGRectGetMaxX(self.industryView1.frame), 360, (KmainScreenBoundsWidth-20)/5, 120);
    Industry_info_listModel *data2 = array[2];
    float percent2 = [data2.ratio floatValue]*100;
    [self.industryView2 setTheTitleWithOrder:[NSString stringWithFormat:@"%d",3] andName:data2.industry_name Percent:[NSString stringWithFormat:@"%0.1f",percent2]];
    
    self.industryView3.frame = CGRectMake(CGRectGetMaxX(self.industryView2.frame), 360, (KmainScreenBoundsWidth-20)/5, 120);
    Industry_info_listModel *data3 = array[3];
    float percent3 = [data3.ratio floatValue]*100;
    [self.industryView3 setTheTitleWithOrder:[NSString stringWithFormat:@"%d",4] andName:data3.industry_name Percent:[NSString stringWithFormat:@"%0.1f",percent3]];
    
    self.industryView4.frame = CGRectMake(CGRectGetMaxX(self.industryView3.frame), 360, (KmainScreenBoundsWidth-20)/5, 120);
    Industry_info_listModel *data4 = array[4];
    float percent4 = [data4.ratio floatValue]*100;
    [self.industryView4 setTheTitleWithOrder:[NSString stringWithFormat:@"%d",5] andName:data4.industry_name Percent:[NSString stringWithFormat:@"%0.1f",percent4]];
    UILabel *label0 = [[UILabel alloc]initWithFrame:CGRectMake(0, 490, KmainScreenBoundsWidth, 5)];
    label0.backgroundColor = [UIColor colorWithRed:229/256.0 green:229/256.0 blue:229/256.0 alpha:0.8];
    [self.contentView addSubview:label0];
    self.title3.frame = CGRectMake(0, 500, KmainScreenBoundsWidth, 30);
    self.xyGraph2.frame = CGRectMake(0, 0, KmainScreenBoundsWidth, 240);
    self.hostView2.frame = CGRectMake(0, 530, KmainScreenBoundsWidth, 240);
    [self prepareTheBarPlot1];
    
}


-(void)makeData{
    
    if (self.model != nil) {

    self.dataDictionary = [[NSMutableDictionary alloc]init];
    NSArray *array0 = self.model.major_salary_year_list;
    NSArray *array1 = self.model.city_list;

    NSMutableArray *dataArray = [[NSMutableArray alloc]init];
       
            for (int i = 0; i< array0.count; i++) {
                id x = [NSNumber numberWithInt:i];
                Major_salary_year_listModel *salaryModel = array0[i];
                float value = ([salaryModel.salary floatValue]/1000);
                id y = [NSNumber numberWithFloat:value];
                
                NSDictionary *dict = @{@"x":x,@"y":y};
                [dataArray addObject:dict];
        }
      
    self.cityDataArr = [[NSMutableArray alloc]init];
    NSMutableArray *cityData =[[NSMutableArray alloc]init];
                  for (int i = 0; i < array1.count; i ++) {
                City_listModel *model = array1[i];
                NSString *string = model.city_name;
                [self.cityDataArr addObject:string];
                id x = [NSNumber numberWithInt:(i +1)];
                id y = [NSNumber numberWithFloat:[model.ratio floatValue]*100];
                NSDictionary *dict = @{@"x":x,@"y":y};
                
                [cityData addObject:dict];
        }
       
    [self.dataDictionary setObject:dataArray forKey:@"dataLine"];
    [self.dataDictionary setObject:cityData forKey:@"barPlot"];
    }
}



-(void)prepareTheGraph{

    self.xyGraph1 = [[CPTXYGraph alloc]initWithFrame:self.hostView1.frame xScaleType:CPTScaleTypeLinear yScaleType:CPTScaleTypeLinear];
    self.hostView1.hostedGraph = self.xyGraph1;
    self.xyGraph1.plotAreaFrame.paddingTop    = CPTFloat(5);
    self.xyGraph1.plotAreaFrame.paddingRight  = CPTFloat(10);
    self.xyGraph1.plotAreaFrame.paddingBottom = CPTFloat(30);
    self.xyGraph1.plotAreaFrame.paddingLeft   = CPTFloat(40);
    self.xyGraph1.plotAreaFrame.masksToBorder  = NO;
    CPTXYPlotSpace *xyPlotSpace = (CPTXYPlotSpace *)self.xyGraph1.defaultPlotSpace;
    int count = (int)self.model.major_salary_year_list.count;
    xyPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:count+1]];
    xyPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:20]];
    xyPlotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:20]];
    CPTXYAxisSet *xyAxisSet = (CPTXYAxisSet *)self.xyGraph1.axisSet;
    CPTXYAxis *xAxis = xyAxisSet.xAxis;
    CPTXYAxis *yAxis = xyAxisSet.yAxis;
    xAxis.majorIntervalLength = [NSNumber numberWithInt:2];
    yAxis.majorIntervalLength = [NSNumber numberWithInt:2];
    xAxis.title = @"毕业年份";
    yAxis.title = @"工资( k )";
    xAxis.minorTicksPerInterval = 1;
    yAxis.minorTicksPerInterval = 1;
    CPTMutableLineStyle *customLineStyle = [CPTMutableLineStyle lineStyle];
    customLineStyle.lineColor = [CPTColor orangeColor];
    customLineStyle.lineWidth = 1.0f;
    NSArray * exclusionRangesArray = [NSArray arrayWithObjects:[CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:count+1] length:[NSNumber numberWithInt:count+1]], nil];
    xAxis.labelExclusionRanges = exclusionRangesArray;
    NSArray *exclusion = [NSArray arrayWithObjects:[CPTPlotRange plotRangeWithLocation:[NSNumber numberWithFloat:20] length:[NSNumber numberWithFloat:20]], nil];
    yAxis.labelExclusionRanges = exclusion;
    CPTLineCap *lineCap = [[CPTLineCap alloc]init];
    lineCap.lineStyle = xAxis.axisLineStyle;
    
    lineCap.lineCapType =  CPTLineCapTypeEllipse;
    xAxis.axisLineCapMin = lineCap;
    lineCap.lineCapType = CPTLineCapTypeSweptArrow;
    CPTLineCap *arrowLineCap = [[CPTLineCap alloc]init];
    arrowLineCap.lineStyle = xAxis.axisLineStyle;
    arrowLineCap.lineCapType = CPTLineCapTypeSweptArrow;
    arrowLineCap.fill = [CPTFill fillWithColor:[CPTColor redColor]];
    xAxis.axisLineCapMax = arrowLineCap;
    yAxis.axisLineCapMax = arrowLineCap;
    CPTScatterPlot *dataLine = [[CPTScatterPlot alloc]init];
    dataLine.identifier = @"dataLine";
    dataLine.dataLineStyle = customLineStyle;
    dataLine.dataSource = self;
    [self.xyGraph1 reloadData];
    dataLine.cachePrecision = CPTPlotCachePrecisionDouble;
    dataLine.interpolation = CPTScatterPlotInterpolationCurved;
    CPTPlotSymbol *plotSymbol = [CPTPlotSymbol ellipsePlotSymbol];
    plotSymbol.size = CGSizeMake(3, 3);
    plotSymbol.lineStyle = customLineStyle;
    plotSymbol.fill = [CPTFill fillWithColor:[CPTColor orangeColor]];
    dataLine.plotSymbol = plotSymbol;

    CPTMutableTextStyle *textStyle=[CPTMutableTextStyle textStyle];
    textStyle.color=[CPTColor orangeColor];
    textStyle.fontSize=10;
    dataLine.labelTextStyle = textStyle;
    [self.xyGraph1 addPlot:dataLine];
    
}


-(void)prepareTheBarPlot1{

    self.xyGraph2 = [[CPTXYGraph alloc]initWithFrame:self.hostView2.frame xScaleType:CPTScaleTypeLinear yScaleType:CPTScaleTypeLinear];
    self.hostView2.hostedGraph = self.xyGraph2;
    self.xyGraph2.plotAreaFrame.paddingTop    = CPTFloat(5);
    self.xyGraph2.plotAreaFrame.paddingRight  = CPTFloat(10);
    self.xyGraph2.plotAreaFrame.paddingBottom = CPTFloat(30);
    self.xyGraph2.plotAreaFrame.paddingLeft   = CPTFloat(40);
    self.xyGraph2.plotAreaFrame.masksToBorder  = NO;
    CPTXYPlotSpace *xyPlotSpace = (CPTXYPlotSpace *)self.xyGraph2.defaultPlotSpace;
    xyPlotSpace.xRange = [CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:(int)self.model.city_list.count+1]];
    xyPlotSpace.yRange = [CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:50]];
    xyPlotSpace.globalYRange = [CPTPlotRange plotRangeWithLocation:[NSNumber numberWithInt:0] length:[NSNumber numberWithInt:50]];
    CPTXYAxisSet *xyAxisSet = (CPTXYAxisSet *)self.xyGraph2.axisSet;
    CPTXYAxis *xAxis = xyAxisSet.xAxis;
    CPTXYAxis *yAxis = xyAxisSet.yAxis;
    
    if (self.cityDataArr != nil) {
    NSMutableArray *xAxisLabels =[NSMutableArray arrayWithArray:self.cityDataArr];
    NSUInteger LabelLocation = 0;
        NSMutableArray *customTickLocations = [[NSMutableArray alloc]init];
        for (int i = 1; i < self.model.city_list.count+1; i++) {
           
            [customTickLocations addObject:[NSNumber numberWithInt:i]];
        }
    NSMutableArray *customLabels = [NSMutableArray arrayWithCapacity:[xAxisLabels count]];
    for (NSNumber *tickLocation in customTickLocations) {
        CPTAxisLabel *newLabel = [[CPTAxisLabel alloc] initWithText:[xAxisLabels objectAtIndex:LabelLocation ++] textStyle:xAxis.labelTextStyle];
        newLabel.tickLocation = tickLocation;
        newLabel.offset = 0;
        [customLabels addObject:newLabel];
    }
    xAxis.axisLabels = [NSSet setWithArray:customLabels];
    
    }
    xAxis.labelingPolicy = CPTAxisLabelingPolicyNone;
    yAxis.majorIntervalLength = [NSNumber numberWithInt:10];
    xAxis.title = @"城市";
    yAxis.title = @"就业百分比( % )";
    yAxis.minorTicksPerInterval = 1;
    CPTMutableLineStyle *customLineStyle = [CPTMutableLineStyle lineStyle];
    customLineStyle.lineColor = [CPTColor orangeColor];
    customLineStyle.lineWidth = 1.0f;

    NSArray *exclusion = [NSArray arrayWithObjects:[CPTPlotRange plotRangeWithLocation:[NSNumber numberWithFloat:50] length:[NSNumber numberWithFloat:50]], nil];
    yAxis.labelExclusionRanges = exclusion;
    CPTLineCap *lineCap = [[CPTLineCap alloc]init];
    lineCap.lineStyle = xAxis.axisLineStyle;
    
    lineCap.lineCapType =  CPTLineCapTypeEllipse;
    lineCap.lineCapType = CPTLineCapTypeSweptArrow;
    CPTLineCap *arrowLineCap = [[CPTLineCap alloc]init];
    arrowLineCap.lineStyle = xAxis.axisLineStyle;
    arrowLineCap.lineCapType = CPTLineCapTypeSweptArrow;
    arrowLineCap.fill = [CPTFill fillWithColor:[CPTColor redColor]];
    xAxis.axisLineCapMax = arrowLineCap;
    yAxis.axisLineCapMax = arrowLineCap;
    
    //画柱状图
    CPTBarPlot *barPlot = [CPTBarPlot tubularBarPlotWithColor:[CPTColor orangeColor] horizontalBars:NO];
    
    barPlot.baseValue = [NSNumber numberWithFloat:1];
    barPlot.barWidth = [NSNumber numberWithFloat:0.3];
    CPTGradient *fillGradient = [CPTGradient gradientWithBeginningColor:[CPTColor orangeColor] endingColor:[CPTColor colorWithComponentRed:1 green:0.5 blue:0 alpha:0.6]];
    fillGradient.angle = CPTFloat(1 ? -90.0 : 0.0);
    barPlot.fill = [CPTFill fillWithGradient:fillGradient];
    customLineStyle.lineColor = [CPTColor clearColor];
    barPlot.lineStyle = customLineStyle;
    barPlot.dataSource = self;
    barPlot. identifier = @"barPlot";
    CPTMutableTextStyle *textStyle = [CPTMutableTextStyle textStyle];
    textStyle.color=[CPTColor orangeColor];
    textStyle.fontSize=10;
    barPlot.labelTextStyle=textStyle;
    barPlot.baseValue = [NSNumber numberWithInt:0];
    // 添加图形到绘图空间
    [self.xyGraph2 addPlot:barPlot toPlotSpace:xyPlotSpace];

}


-(NSUInteger)numberOfRecordsForPlot:(nonnull CPTPlot *)plot{

    NSString *identifier = (NSString *)plot.identifier;
    if ([identifier isEqualToString:@"dataLine"]) {
        return self.model.major_salary_year_list.count;
    }
    return 5;

}


-(id)numberForPlot:(CPTPlot *)plot field:(NSUInteger)fieldEnum recordIndex:(NSUInteger)idx{
    
    NSNumber *num = nil;
    NSString *identifier = (NSString *)plot.identifier;
    NSArray *dataArr = [self.dataDictionary objectForKey:identifier];
    num = dataArr[idx][(fieldEnum == CPTScatterPlotFieldX ? @"x" : @"y")];
    
    return num;
    
}





@end
